package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class ProductCategoryPage extends Utils {
    public static String url = "http://49.235.61.184/?s=category/index.html";


    public ProductCategoryPage() {
        super(url);
    }
    /**
     * 测试一：检查页面
     */
    public void checkProductCategoryPage() throws IOException, InterruptedException {
        //检查”全部分类页面“按钮
        driver.findElement(By.cssSelector("#goods-category > div > a > div > span"));
        //检查商品分类栏
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-top > div > div > div > div > ul"));
        //检查每一类中的分类
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-left > div > div > ul"));
        //检查这类商品展示模块
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-item > div > ul"));
        //检查购物车图标
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-item > div > ul > li:nth-child(1) > div > div > div.bottom-content.am-text-truncate > i"));
        //检查分页框
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-page > ul"));
        //检查有多少数据、共几页
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-page > div"));
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());


        //检查按钮
        //点击时尚服饰，分类跳转时尚服饰
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-top > div > div > div > div > ul > li:nth-child(2) > div")).click();
        //点击女装
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-left > div > div > ul > li:nth-child(2)")).click();
        //点击连衣裙
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-title.am-scrollable-horizontal > div > a.am-radius.active")).click();
        Thread.sleep(1000);
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());


    }

    /**
     *测试二：测试加入购物车
     */
    public void shoppingCart() throws IOException, InterruptedException {

        //获取加入购物车数量
        String num = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a > strong")).getText();

        //直接加入购物车
        //点击”名品潮包“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-top > div > div > div > div > ul > li:nth-child(3) > div")).click();
        Thread.sleep(1000);
        //点击”购物车“图标按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-item > div > ul > li:nth-child(1) > div > div > div.bottom-content.am-text-truncate > i")).click();
        Thread.sleep(1000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //获取加入购物车数量*/
        String num1 = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a > strong")).getText();
        assert !num1.equals(num);




         //点击时尚服饰，分类跳转时尚服饰
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-top > div > div > div > div > ul > li:nth-child(2) > div")).click();
        //点击女装
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-left > div > div > ul > li:nth-child(2)")).click();
        //点击连衣裙
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-title.am-scrollable-horizontal > div > a.am-radius.active")).click();
        Thread.sleep(1000);

      //点击购物车图标
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-item > div > ul > li:nth-child(1) > div > div > div.bottom-content.am-text-truncate > i")).click();
        //定位iframe元素
        WebElement webElement = driver.findElement(By.cssSelector("iframe.am-block"));
        //切换到iframe子页面
        driver = driver.switchTo().frame(webElement);

        //选择尺码
        driver.findElement(By.cssSelector("div.spec-options.sku-items.am-radius ul li:nth-child(1)")).click();
        //修改数量
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > form > div.goods-spec-container.am-padding-sm > div > div.spec-options.am-margin-top-sm > div.am-input-group.am-input-group-sm.number-operate > span:nth-child(3)")).click();
        //屏幕截图
        Thread.sleep(1000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //点击”加入购物车“
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example")).click();
        //屏幕截图
        Thread.sleep(1000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        //切换回父页面的默认上下文
        driver.switchTo().defaultContent();

        //获取加入购物车数量
        String num2 = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a > strong")).getText();
        assert !num2.equals(num1);
    }
    /**
     *测试三：分页、跳转页
     */
    public void testPaging() throws IOException, InterruptedException {
        //driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > header > div > a > div")).click();
        //定位输入框元素
        WebElement webElement = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-page > ul > span.current-page-input > input"));
        //双击选中输入框所有文本 → 按DELETE删除 → 执行操作
        new Actions(driver).doubleClick(webElement).sendKeys(Keys.DELETE).perform();
        //输入新值+回车
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-page > ul > span.current-page-input > input")).sendKeys("2",Keys.ENTER);
        //屏幕截图
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        //设置跳转第几个页面
        webElement = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-page > ul > span.to-page-input > input"));
        new Actions(driver).doubleClick(webElement).sendKeys(Keys.DELETE).perform();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-page > ul > span.to-page-input > input")).sendKeys("3",Keys.ENTER);
        //屏幕截图
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        Thread.sleep(3000);
        //页面跳转

        //点击”>>“按钮 - 尾页
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-page > ul > li:nth-child(6)")).click();
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        //点击”<<“按钮 - 首页
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.category-list-container.category-list-level-0-container.am-radius > div > div.zero-search-right > div > div.zero-right-page > ul > li:nth-child(1)")).click();
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());






    }
}
